package com.zhongxi.guoyuan.adapter.seller;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.seller.ComplaintData;
import com.zhongxi.guoyuan.entity.seller.PriceData;
import com.zhongxi.guoyuan.entity.user.UserData;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/12/13.
 */

public class SellerCoinOrderComplainAdapter extends BaseAdapter {

    private Context context;
    private List<ComplaintData> data;
    private JSONObject obj;

    public SellerCoinOrderComplainAdapter(Context context, List<ComplaintData> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_coin_complain, parent, false);
            holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvPhone = (TextView) convertView.findViewById(R.id.tvPhone);
            holder.tvMoney = (TextView) convertView.findViewById(R.id.tvMoney);
            holder.ivCall = (ImageView) convertView.findViewById(R.id.ivCall);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        int pos = position+1;
        holder.tvNum.setText("第"+pos+"起投诉");
        if(null!=data.get(position)&&!"".equals(data.get(position))){
            ComplaintData list = data.get(position);
            if(null!=list.getCtime()&&!"".equals(list.getCtime())){
                holder.tvTime.setText(list.getCtime());
            }else{
                holder.tvTime.setText("");
            }
            if(null!=list.getContent()&&!"".equals(list.getContent())){
                holder.tvContent.setText(list.getContent());
            }else{
                holder.tvContent.setText("");
            }
            if(null!=obj&&!"".equals(obj)){
                try{
                    Gson gson = new Gson();
                    if(null!=list.getOrderid()&&!"".equals(list.getOrderid())){
                        String price = obj.getJSONObject("price").getString(list.getOrderid());
                        PriceData priceData = gson.fromJson(price, PriceData.class);
                        if(null!=priceData.getPrice()&&!"".equals(priceData.getPrice())){
                            holder.tvMoney.setText("本次消费："+priceData.getPrice()+"银币");
                        }else{
                            holder.tvMoney.setText("本次消费：*银币");
                        }
                    }
                    if(null!=list.getUserid()&&!"".equals(list.getUserid())) {
                        String user = obj.getJSONObject("user").getString(list.getUserid());
                        final UserData userData = gson.fromJson(user, UserData.class);
                        if (null != userData.getUsername() && !"".equals(userData.getUsername())) {
                            holder.tvName.setText(userData.getUsername());
                        } else {
                            holder.tvName.setText("");
                        }
                        if (null != userData.getMphone() && !"".equals(userData.getMphone())) {
                            holder.tvPhone.setText(userData.getMphone());
                            holder.ivCall.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    /*Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + userData.getMphone()));
                                    context.startActivity(intent);*/
                                    Intent intent = new Intent(Intent.ACTION_DIAL);
                                    Uri data = Uri.parse("tel:" + userData.getMphone());
                                    intent.setData(data);
                                    context.startActivity(intent);
                                }
                            });
                        } else {
                            holder.tvPhone.setText("");
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        return convertView;
    }

    public void setJSONObject(JSONObject obj) {
        this.obj = obj;
    }

    class ViewHolder{
        private TextView tvNum;
        private TextView tvTime;
        private TextView tvContent;
        private TextView tvName;
        private TextView tvPhone;
        private TextView tvMoney;
        private ImageView ivCall;
    }
}
